/*
 * AudioChartPanel.java
 *
 * Created on 2007年12月26日, 上午10:18
 */
package com.hadeslee.yoyoplayer.setting;

import com.hadeslee.yoyoplayer.util.AudioChart;
import com.hadeslee.yoyoplayer.util.Config;
import com.hadeslee.yoyoplayer.util.Util;
import java.awt.Color;
import javax.swing.JColorChooser;

/**
 *
 * @author  hadeslee
 */
public class AudioChartPanel extends javax.swing.JPanel implements Initable {

    private Color topColor,  centerColor,  bottomColor,  color;
    private Color lineColor;
    public static final int width = 15;
    public static final int height = 15;

    /** Creates new form AudioChartPanel */
    public AudioChartPanel() {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        displayMode = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        fps = new javax.swing.JSlider();
        info = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        speed = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        band = new javax.swing.JSlider();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(Config.getResource("AudioChartPanel.option"))); // NOI18N

        jLabel1.setText(Config.getResource("AudioChartPanel.analyzing")); // NOI18N

        displayMode.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "频谱分析", "示波显示", "不显示" }));

        jLabel2.setText(Config.getResource("AudioChartPanel.speed")); // NOI18N

        fps.setMajorTickSpacing(10);
        fps.setMaximum(50);
        fps.setMinimum(10);
        fps.setMinorTickSpacing(1);
        fps.setPaintLabels(true);
        fps.setPaintTicks(true);
        fps.setSnapToTicks(true);
        fps.setValue(25);
        fps.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                fpsStateChanged(evt);
            }
        });

        info.setText(Config.getResource("AudioChartPanel.fps")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(displayMode, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(info)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(displayMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(fps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(info)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(Config.getResource("AudioChartPanel.line"))); // NOI18N

        jLabel4.setText(Config.getResource("AudioChartPanel.colorMethod")); // NOI18N

        jButton1.setText(Config.getResource("AudioChartPanel.peak")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(Config.getResource("AudioChartPanel.top")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(Config.getResource("AudioChartPanel.center")); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(Config.getResource("AudioChartPanel.bottom")); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addContainerGap(40, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(Config.getResource("asdf"))); // NOI18N

        jLabel5.setText(Config.getResource("AudioChartPanel.colorMethod")); // NOI18N

        jButton5.setText(Config.getResource("AudioChartPanel.theLine")); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel6.setText(Config.getResource("AudioChartPanel.disappearSpeed")); // NOI18N

        speed.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "较快", "普通", "较慢" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton5))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(speed, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(speed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jButton9.setText(Config.getResource("AudioChartPanel.save")); // NOI18N
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton9);

        jButton10.setText(Config.getResource("reset")); // NOI18N
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton10);

        jLabel3.setText(Config.getResource("AudioChartPanel.numOfBars")); // NOI18N

        band.setMajorTickSpacing(10);
        band.setMaximum(40);
        band.setMinimum(10);
        band.setMinorTickSpacing(1);
        band.setPaintLabels(true);
        band.setPaintTicks(true);
        band.setSnapToTicks(true);
        band.setValue(20);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(band, 0, 0, Short.MAX_VALUE)
                            .addComponent(jLabel3))))
                .addContainerGap())
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(band, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Color c = JColorChooser.showDialog(this, Config.getResource("AudioChartPanel.peakColor"), color);
        if (c != null) {
            color = c;
            jButton1.setIcon(Util.createColorIcon(c, width, height));
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:.
        Color c = JColorChooser.showDialog(this, Config.getResource("AudioChartPanel.topColor"), topColor);
        if (c != null) {
            topColor = c;
            jButton2.setIcon(Util.createColorIcon(c, width, height));
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Color c = JColorChooser.showDialog(this, Config.getResource("AudioChartPanel.centerColor"), centerColor);
        if (c != null) {
            centerColor = c;
            jButton3.setIcon(Util.createColorIcon(c, width, height));
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        Color c = JColorChooser.showDialog(this, Config.getResource("AudioChartPanel.bottomColor"), bottomColor);
        if (c != null) {
            bottomColor = c;
            jButton4.setIcon(Util.createColorIcon(c, width, height));
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        Color c = JColorChooser.showDialog(this, Config.getResource("AudioChartPanel.lineColor"), lineColor);
        if (c != null) {
            lineColor = c;
            jButton5.setIcon(Util.createColorIcon(c, width, height));
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        doSave();
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
        init();
    }//GEN-LAST:event_jButton10ActionPerformed

    private void fpsStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_fpsStateChanged
        // TODO add your handling code here:
        info.setText(fps.getValue() + Config.getResource("AudioChartPanel.ffps"));
    }//GEN-LAST:event_fpsStateChanged

    public void init() {
        Config config = Config.getConfig();
        displayMode.setSelectedItem(getStringOfDisplayMode(config.getAudioChartDisplayMode()));
        fps.setValue(config.getAudioChartfps());
        band.setValue(config.getAudioChartBarCount());
        info.setText(config.getAudioChartfps() + Config.getResource("AudioChartPanel.ffps"));
        topColor = config.getAudioChartTopColor();
        centerColor = config.getAudioChartCenterColor();
        bottomColor = config.getAudioChartbottomColor();
        color = config.getAudioChartPeakColor();
        lineColor = config.getAudioChartlineColor();
        jButton1.setIcon(Util.createColorIcon(color, width, height));
        jButton2.setIcon(Util.createColorIcon(topColor, width, height));
        jButton3.setIcon(Util.createColorIcon(centerColor, width, height));
        jButton4.setIcon(Util.createColorIcon(bottomColor, width, height));
        jButton5.setIcon(Util.createColorIcon(lineColor, width, height));
        speed.setSelectedItem(getStringOfSpeed(config.getAudioChartDisappearSpeed()));
    }

    private int getIntOfDisplayMode(String s) {
        if (s.equals("频谱分析")) {
            return AudioChart.DISPLAY_MODE_SPECTRUM_ANALYSER;
        } else if (s.equals("示波显示")) {
            return AudioChart.DISPLAY_MODE_SCOPE;
        } else if (s.equals("不显示")) {
            return AudioChart.DISPLAY_MODE_OFF;
        }
        return AudioChart.DISPLAY_MODE_SPECTRUM_ANALYSER;
    }

    private String getStringOfDisplayMode(int mode) {
        switch (mode) {
            case AudioChart.DISPLAY_MODE_OFF:
                return "不显示";
            case AudioChart.DISPLAY_MODE_SCOPE:
                return "示波显示";
            case AudioChart.DISPLAY_MODE_SPECTRUM_ANALYSER:
                return "频谱分析";
        }
        return "频谱分析";
    }

    private int getIntOfSpeed(String s) {
        if (s.equals("较慢")) {
            return Config.DISAPPEAR_SLOW;
        } else if (s.equals("较快")) {
            return Config.DISAPPEAR_QUICK;
        } else if (s.equals("普通")) {
            return Config.DISAPPEAR_NORMAL;
        }
        return Config.DISAPPEAR_NORMAL;
    }

    private String getStringOfSpeed(int mode) {
        switch (mode) {
            case Config.DISAPPEAR_NORMAL:
                return "普通";
            case Config.DISAPPEAR_QUICK:
                return "较快";
            case Config.DISAPPEAR_SLOW:
                return "较慢";
        }
        return "普通";
    }

    private void doSave() {
        Config config = Config.getConfig();
        config.setAudioChartDisplayMode(getIntOfDisplayMode(displayMode.getSelectedItem().toString()));
        config.setAudioChartfps(fps.getValue());
        config.setAudioChartCenterColor(centerColor);
        config.setAudioChartTopColor(topColor);
        config.setAudioChartPeakColor(color);
        config.setAudioChartlineColor(lineColor);
        config.setAudioChartbottomColor(bottomColor);
        config.setAudioChartDisappearSpeed(getIntOfSpeed(speed.getSelectedItem().toString()));
        config.setAudioChartBarCount(band.getValue());
        config.getPlayer().getAudioChart().setPeakColor(color);
        config.getPlayer().getAudioChart().setScopeColor(lineColor);
        config.getPlayer().getAudioChart().setSpectrumAnalyserBandCount(band.getValue());
        config.getPlayer().getAudioChart().setSpectrumAnalyserColors(Util.getColors(bottomColor, centerColor, topColor, 256));
        config.getPlayer().getAudioChart().setDisplayMode(config.getAudioChartDisplayMode());
        if (config.isAutoCloseDialogWhenSave()) {
            config.getOptionDialog().setVisible(false);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSlider band;
    private javax.swing.JComboBox displayMode;
    private javax.swing.JSlider fps;
    private javax.swing.JLabel info;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JComboBox speed;
    // End of variables declaration//GEN-END:variables
}
